package org.example.j2024_11_9;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/session")
public class SessionController {

   @RequestMapping("/set")
   public String set(HttpServletRequest request){
       HttpSession session = request.getSession() ;
      session.setAttribute("name" , "zhangsan");
      session.setAttribute("id" , 1);
      return "session设置成功" ;
   }
   @RequestMapping("/s1")
   public String s1(HttpSession session){
      String str  = (String) session.getAttribute("name");
      return str ;
   }
   @RequestMapping("/s2")
   public String s2(@SessionAttribute(value = "name" , required = false) String name ,
                    @SessionAttribute(value = "id",required = false) int id){
      return "name "+ name + "  id "+id ;

   }


}
